package com.lkm.market2_0.m_imple.steelyarddata;

import android.support.annotation.NonNull;

import com.lkm.javatool.DateHelp;
import com.lkm.javatool.ServerTimeSync;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.OrganizationInfo;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m_imple.steelyardserver.server.Config;

import java.util.Locale;



class CreateOrderNum {

    public static String callorderNum(boolean isFullOrder, SaleCacheInfo saveSaleCacheInfo, SteelyardInfo steelyardInfo) {
        String orderNum;

        
        if (saveSaleCacheInfo.isHistory()) {
            String steelyardMac = steelyardInfo.getSteelyardMac();
            orderNum = createOrderNumBySt(saveSaleCacheInfo, steelyardMac);
            return orderNum;
        }

        if (isFullOrder) {

            if (steelyardInfo.codetype == SteelyardInfo.codetype_0) {
                
                orderNum = createOrderNumByOrg(steelyardInfo.getOrg());
            } else {
                
                String sourceTime = saveSaleCacheInfo.isUseThisDate? saveSaleCacheInfo.getDate():
                        DateHelp.DateFormatyyyy_MM_dd_HHmmss(Locale.getDefault()).format(ServerTimeSync.getInstance().getServerCurTime());

                
                orderNum = "1" + (sourceTime.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "").replaceAll("/", "").substring(2)) + steelyardInfo.getB_id();
            }

        } else {
            
            orderNum = "";
        }

        return orderNum;
    }

    

    @NonNull
    private static String createOrderNumBySt(SaleCacheInfo saveSaleCacheInfo, String steelyardMac) {
        String orderNum = "";
        if (steelyardMac.length() >= 10) {
            orderNum += steelyardMac.substring(steelyardMac.length() - 10);
        } else {
            orderNum += steelyardMac;
        }
        String date = saveSaleCacheInfo.getDate().replaceAll(":", "").replaceAll(" ", "").replaceAll("/", "").replaceAll("-", "");
        if (date.length() >= 10) {
            orderNum += date.substring(date.length() - 10);
        } else {
            orderNum += date;
        }
        return orderNum;
    }

    

    private static String createOrderNumByOrg(int organizationId) {
        OrganizationInfo organizationInfo = AppDataCache.getInstance().getOrganizationEntity(organizationId);

        
        String nodeCode = organizationInfo.getNodeCode();

        if (ExpandConfig.instance.isTest()) {
            
            String time = ("" + ServerTimeSync.getInstance().getServerCurTime());
            if (time.length() > 10) {
                time = time.substring(time.length() - 10);
            }
            return nodeCode + "0" + time;
        }


        String nextOrderCount = "";

//            nodeCode = com.lkm.market2_0.Config.instance.getDefaultNodeCode();
//            nextOrderCount=com.lkm.market2_0.Config.instance.getDefaultNextOrderCount();

        
        nextOrderCount = "" + organizationInfo.nextOrderCount();



        return nodeCode + nextOrderCount;
    }


}
